#ifndef _PEER_H_
#define _PEER_H_

#include <stdlib.h>
#include <math.h>
#include <string>
#include "torrent.h"
#include "net.h"
#include "storage.h"
#include "piece.h"

class Torrent;

class Peer
{
public:
	// Peer Data
	string GetIP();
	string GetPort();
	Piece* GetCurrentPiece();

	// BitTorrent Actions
	bool Connect();
	bool Disconnect();
	bool DownloadNextPiece();

private:
	unsigned char m_ip[4];
	unsigned short m_port;

	unsigned char *m_bitfield;
	Piece *m_currentpiece;
	Torrent *m_torrent;
	bool m_valid;
	int m_tmp;
	int m_socket;

	// Constructors are only available to Tracker class
	friend class Tracker;
	friend class Torrent;
	Peer(Torrent *torrent, unsigned char ip[4], unsigned short port);
	~Peer();

	// Message Handling
	bool HandleHandshakeMessage();
	bool HandleChokeMessage(unsigned message_length);
	bool HandleUnchokeMessage(unsigned message_length);
	bool HandleInterestedMessage(unsigned message_length);
	bool HandleNotInterestedMessage(unsigned message_length);
	bool HandleHaveMessage(unsigned message_length);
	bool HandleBitfieldMessage(unsigned message_length);
	bool HandleRequestMessage(unsigned message_length);
	bool HandlePieceMessage(unsigned message_length);
	bool HandleCancelMessage(unsigned message_length);
};

#endif
